/**
 * 轨迹回放器
 */
var TrackReplay = function (mapObj, mapBus, lnglatArr) {
    /**
     * 当轨迹回放到达一个点的时候触发这个事件
     */
    this.onTrackReplayArrive = null;
    /**
     * 当轨迹回放正在进行中的时候触发这个事件
     */
    this.onTrackReplaying = null;
    /**
     * 当轨迹回完毕的时候触发这个事件
     */
    this.onTrackReplayComplete = null;
    /**
     * 当播放序号变化时
     */
    this.onPlayIndexChange = null;
    /**
     * 播放总序号
     */
    this.totalIndex = lnglatArr.length;
    /**
     * 当前播放序号
     */
    this.playIndex = 0;
    /**
     * 是否保持车辆的居中
     */
    this.isKeepCenter = false;
    this.state = "stop"; // 当前播放状态
    var marker = mapBus.busMarker,
        busNoMaker = mapBus.busNoMarker;//车牌Maker
    var trackReplay = this; // 用于给私有方法使用
    var totalIndex = this.totalIndex; // 轨迹回放中小车回放到第几个点
    var playIndex = this.playIndex; // 轨迹回放中小车回放到第几个点
    var speed = 80; // 轨迹回放的速度
    var lineArr = new Array(); // 将坐标点数组转化成高德地图可用的数组对象
    for (var i = 0; i < lnglatArr.length; i++) { // 画线数组赋值
        lineArr.push(new AMap.LngLat(lnglatArr[i].loLgt, lnglatArr[i].loLtt));
    }
    mapObj.mapMoveToCorrect(lnglatArr[playIndex].loLgt, lnglatArr[playIndex].loLtt); // 移动地图中心点
    marker.setPosition(new AMap.LngLat(lnglatArr[playIndex].loLgt, // 设置小车坐标
        lnglatArr[playIndex].loLtt));


    mapObj.drawTrack(lineArr, "#F00", 2); // 画轨迹线
    AMap.event.addListener(marker, 'moveend', eventTrackReplayArrive);
    AMap.event.addListener(marker, 'moving', eventTrackReplaying);

    /**
     * 开始轨迹回放
     */
    this.startReplay = function (playSpeed) { // 开始播放
        if (trackReplay.state == "play") {
            return;
        }
        trackReplay.state = "play";
        if (trackReplay.onPlayIndexChange) {
            trackReplay.onPlayIndexChange(playIndex, totalIndex);
        }
        speed = playSpeed;
        if (playIndex >= lnglatArr.length - 1) {
            if (trackReplay.onPlayIndexChange) {
                trackReplay.onPlayIndexChange(playIndex, totalIndex);
            }
            if (trackReplay.onTrackReplayComplete) {
                trackReplay.onTrackReplayComplete();
            }
            return;
        }
        playIndex++;
        marker.moveTo(lineArr[playIndex], speed);
    };

    /**
     * 暂停轨迹回放
     */
    this.pauseReplay = function () {
        if (trackReplay.state == "pause") {
            return;
        }
        trackReplay.state = "pause";
        marker.stopMove();
        playIndex--;
    };

    /**
     * 停止轨迹回放
     */
    this.stopReplay = function () {
        playIndex = 0;
        if (trackReplay.state == "stop") {
            return;
        }
        marker.stopMove();
        marker.setPosition(lineArr[playIndex]);
        busNoMaker.setPosition(lineArr[playIndex]);
        mapObj.mapMoveTo(marker.getPosition().getLng(), marker.getPosition().getLat());
        trackReplay.state = "stop";
    };

    this.setBusIndex = function (index, autoPlay) {
        marker.stopMove();
        playIndex = index;
        this.playIndex = index;
        marker.setPosition(lineArr[playIndex]);
        if (trackReplay.onPlayIndexChange) {
            trackReplay.onPlayIndexChange(playIndex, totalIndex);
        }
        if (playIndex >= lnglatArr.length - 1) {
            if (trackReplay.onPlayIndexChange) {
                trackReplay.onPlayIndexChange(playIndex, totalIndex);
            }
            if (trackReplay.onTrackReplayComplete) {
                trackReplay.onTrackReplayComplete();
            }
            return;
        }

        if (autoPlay) {
            playIndex++;
            trackReplay.state = "play";
            marker.moveTo(lineArr[playIndex], speed);
        }
    };

    function eventTrackReplayArrive() {// 小车到达一个轨迹点触发
        if (playIndex >= lnglatArr.length - 1) {
            if (trackReplay.onPlayIndexChange) {
                trackReplay.onPlayIndexChange(playIndex, totalIndex);
            }
            if (trackReplay.onTrackReplayComplete) {// 回放完成
                trackReplay.onTrackReplayComplete();
            }
            return;
        }
        if (trackReplay.onTrackReplayArrive) {
            trackReplay.onTrackReplayArrive(marker, lineArr[playIndex]);
        }
        if (trackReplay.onPlayIndexChange) {
            trackReplay.onPlayIndexChange(playIndex, totalIndex);
        }
        playIndex++;
        if (playIndex <= totalIndex - 1) {
            if (marker.getPosition().getLng() == lineArr[playIndex].getLng()
                && marker.getPosition().getLat() == lineArr[playIndex].getLat()) {
                eventTrackReplayArrive();
            } else {
                marker.moveTo(lineArr[playIndex], speed);
            }
        }
    }

    function eventTrackReplaying() {// 小车运动的时候触发
        if (trackReplay.isKeepCenter) {
            mapObj.mapMoveTo(marker.getPosition().getLng(), marker.getPosition().getLat());
        }
        if (trackReplay.onTrackReplaying) {
            trackReplay.onTrackReplaying(marker);
        }
    }

};
